# *****************************************************************
# OVERVIEW ####
# *****************************************************************
# REGRESSION.R
# Are Rural Attitudes Just Republican?
# Core Functions for Data Analysis
# Jennifer Lin and Kristin Lunz Trujillo
# Created On: 2023 01 27

# *** The code in this file replicates the regression analyses in
#   the paper and includes code on components that are in the 
#   main text. ***

# ** Before running this file, ensure that you have ran
#   - Functions.R
#   - ANES20_CLEAN.R
#  or components of this code will result in an error. See 
#  00_GENERAL.R for more details **

# *****************************************************************
# INFERENTIAL STATISTICS IN MAIN PAPER ####
# *****************************************************************

# The following code generates the tables and figures that are 
#   present in the main paper.

# To start, we extract the variables that we want to regress. 
vars_to_regress <- ANES_Issues %>%
  select(starts_with("I_"))

# To run the regression using survey weights, we declare the data
#   as a survey object and remove Independents
ANES_regress_Data <- ANES_Issues %>% 
  filter(PARTY != "Independent") %>% 
  srvyr::as_survey(weight = V200010a)

# We iterate through each of the variables and apply the model 
#   specified in the paper and in the `issues_model()` function.
I_results <- issues_model(
  vars_to_regress, 1:22, design = ANES_regress_Data)

# Create a vector of terms to label predictors in the models to 
#   use with the regression table exports.
terms <- c(
  "Democrat-Urban",
  "Republican-Rural",
  "Republican-Urban",
  "Trump Supporter",
  "Ideology",
  "Female",
  "Income",
  "Education",
  "Age",
  "Racial Resentment -- Work without Favors",
  "Racial Resentment -- History of Slavery",
  "Racial Resentment -- Blacks get less",
  "Racial Resentment -- Try Harder",
  "Racial Minority",
  "Church"
)

# We can plot the results in a graph, as shown in Figure 3 in the 
#   main paper

# To start, create an empty list to store results from each of the
#   regression models
plot_df <- list()

# To get the estimates and standard erros to plot, we pull these#
#   details from our list of regression models, iterating
#   trhough each model one by one
for (i in 1:length(I_results)) {
  plot_df[[i]] <- tidy(I_results[[i]])
  plot_df[[i]]$variable <- names(vars_to_regress[i])
}

# We then clean up the resulting data frame, renaming the 
#   varaible names as needed
plot_df_clean <- bind_rows(plot_df) %>% 
  filter(grepl("Rural_Party", term)) %>% 
  mutate(
    Issue_Desc = case_when(
      variable == "I_refugees" ~ "Allow Refugees to Come to US",
      variable == "I_citizenship" ~ "Provide a Path to Citizenship",
      variable == "I_deport" ~ "Deporting Immigrants to Native Country",
      variable == "I_SPChild" ~ "Separating Children from Parents at Border",
      variable == "I_reduceIneq" ~ "Reduce Income Inequality",
      variable == "I_ACA" ~ "Approve Affordable Care Act",
      variable == "I_reqVax" ~ "Require Vaccinations",
      variable == "I_RegGHG" ~ "Regulate Greenhouse Gas Emissions",
      variable == "I_bkgCheck" ~ "Background Check for Gun Purchases",
      variable == "I_banAR" ~ "Ban Assault Style Rifles",
      variable == "I_buyBackAR" ~ "Assault Rifle Buy Back",
      variable == "I_Opioid" ~ 'Address Opioid Epidemic',
      variable == "I_FreeTrade" ~ "Allowing Free Trade Agreements",
      variable == "I_UBI" ~ "Provide Citizens 12K A Year",
      variable == "I_incHCspend" ~ "Increasing Spending to Cover Health Care",
      variable == "I_TransMil" ~ "Allow Transgender People to Serve in Military",
      variable == "I_ReqIDVote" ~ "Require ID to Vote",
      variable == "I_FelonVote" ~ "Allowing Felons to Vote",
      variable == "I_JAccess" ~ "Transparency for Journalists",
      variable == "I_FamilyLeave" ~ "Provide Paid Family Leave",
      variable == "I_birthright" ~ "End Birthright Citizenship",
      variable == "I_BorderWall" ~ "Build Wall on Southern Border"
    ),
    Issue_Desc = factor(Issue_Desc, levels = unique(Issue_Desc)),
    term = case_when(
      term == "Rural_PartyDemocrat-Urban" ~ "Democrat-Urban",
      term == "Rural_PartyRepublican-Rural" ~ "Republican-Rural",
      term == "Rural_PartyRepublican-Urban" ~ "Republican-Urban"
    ),
    term = factor(
      term,
      levels = c(
        "Democrat-Urban", "Republican-Rural", "Republican-Urban"
      )
    )
  )

# Finally, we generate the plot
ggplot(plot_df_clean, aes(x = term, y = estimate))+
  geom_pointrange(
    aes(
      ymin = estimate-1.96*std.error, 
      ymax = estimate+1.96*std.error)
  )+
  geom_hline(
    yintercept = 0, color = "grey75", linetype = "dashed")+
  facet_wrap(~Issue_Desc, ncol = 2)+
  coord_flip()+
  labs(
    title = "Favor or Oppose?",
    subtitle = 
      "Evaluations of Core Issues by Place of Residence and Party",
    caption = 
      "Data: American National Elections Studies (ANES) 2020"
  )+
  ylab("Estimate")+
  xlab("Key Predictors")+
  theme_bw()+
  theme(
    plot.title    = element_text(
      hjust = 0.5, size = 20, colour="black", face = "bold"),
    plot.subtitle = element_text(
      hjust = 0.5, size = 16, colour="black"),
    legend.title  = element_text(
      hjust = 0.5, size = 14, colour="black", face = "bold"),
    plot.caption  = element_text(size = 10, colour="black"),
    axis.title    = element_text(size = 14, colour="black"),
    axis.text.x   = element_text(
      size = 12, colour="black", angle = 0, hjust = 0.5)
  )  

